package com.hsyt.pms.support.common;

import com.alibaba.fastjson.JSONObject;
import com.hsyt.pms.api.common.DictionaryVo;
import com.hsyt.pms.api.common.PmsEnumValue;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @ClassName: EnumCacheHelper
 * @Package: com.hsyt.pfc.support.common
 * @Author: wangze@hsyuntai.com
 * @Date: 2018年9月29日 10:15
 * @Copyright: 版权归 HSYUNTAI 所有
 */
public class EnumCacheHelper {

    private static StringRedisTemplate stringRedisTemplate;

    @Resource
    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        if (null == stringRedisTemplate) {
            throw new IllegalArgumentException("stringRedisTemplate not null");
        }
        EnumCacheHelper.stringRedisTemplate = stringRedisTemplate;
    }

    public static void syncEnumDict(List<PmsEnumValue> enumValues) {
        if (!CollectionUtils.isEmpty(enumValues)) {
            Map<String, List<PmsEnumValue>> enumMaps =
                    enumValues.stream().collect(Collectors.groupingBy(PmsEnumValue::getEnumType));
            Map<String, Map<String, String>> enumMap = enumMaps.entrySet().stream().collect(
                    Collectors.toMap(Map.Entry::getKey, value -> value.getValue().stream().collect(
                            Collectors.toMap(PmsEnumValue::getEnumValue, PmsEnumValue::getEnumTxt, (key1, key2) -> key1)),
                            (key1, key2) -> key1));
            enumMap.forEach((type, value) -> {
                stringRedisTemplate.delete("CFG_ENUM_DICT" + type);
            });
            enumMap.forEach((type, value) -> {
                stringRedisTemplate.opsForHash().putAll("CFG_ENUM_DICT" + type, value);
            });
        }
    }

    public static void syncDictionary(List<DictionaryVo> dictVos) {
        if (!CollectionUtils.isEmpty(dictVos)) {
            Map<String, List<DictionaryVo>> dictMaps =
                    dictVos.stream().collect(Collectors.groupingBy(DictionaryVo::getGroupCode));

            Map<String, Map<String, String>> dictMap = dictMaps.entrySet().stream().collect(
                    Collectors.toMap(Map.Entry::getKey, value -> value.getValue().stream().collect(
                            Collectors.toMap(DictionaryVo::getDictCode,
                                    dict -> {
                                        return JSONObject.toJSONString(new HashMap<String, String>() {
                                            {
                                                put("name", dict.getDictName());
                                                put("ext", dict.getExtValue());
                                            }
                                        });
                                    }, (key1, key2) -> key1)),
                            (key1, key2) -> key1));
            dictMap.forEach((groupCode, value) -> {
                stringRedisTemplate.delete("CFG_SYS_DICT" + groupCode);
            });
            dictMap.forEach((groupCode, value) -> {
                stringRedisTemplate.opsForHash().putAll("CFG_SYS_DICT" + groupCode, value);
            });
        }
    }

}
